package earogya.pharmacy.pharmacy.messages;

import java.io.IOException;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.message.OMN_O07;
import ca.uhn.hl7v2.model.v251.segment.ORC;
import ca.uhn.hl7v2.model.v251.segment.RQD;
import earogya.pharmacy.pharmacy.Non_Stock_Requisition;
import earogya.pharmacy.pharmacy.PharmacyHibernateManager;
import java.util.Date;

/*OMN^O07 (non stock requisition order) message handler
 * 
 * deals when  a new drug is received to the pharmacy
 * reads the OMN^O07 message and extract data from it and
 * sends the details to the store database using hibernate
 * 
 * */
public class OMN_O07_Handler {
	
	private OMN_O07 adrMsg;
	private PharmacyHibernateManager hibernate_manager = new PharmacyHibernateManager();
	private Non_Stock_Requisition req = new Non_Stock_Requisition();

	public OMN_O07_Handler(OMN_O07 adrMsg) {
		this.adrMsg = adrMsg;
	}

	public String manageMessage() throws HL7Exception, IOException {
		String ack = null;
		try {
			ack = adrMsg.generateACK().encode();
			this.getRequisitionDetails();
		} catch (HL7Exception e) {
			ack = adrMsg.generateACK("OE", e).encode();
		}
		return ack;
	}
	
	public void getRequisitionDetails(){
		
		ORC orc = adrMsg.getORDER().getORC();
		RQD rqd = adrMsg.getORDER().getRQD();
		if(orc.getOrc2_PlacerOrderNumber().getEi1_EntityIdentifier().getValue()!=null)
			req.setRequisitionID(orc.getOrc2_PlacerOrderNumber().getEi1_EntityIdentifier().getValue());
		if(orc.getOrc10_EnteredBy(0).getIDNumber().getValue()!=null)
			req.setEnteredByID(orc.getOrc10_EnteredBy(0).getIDNumber().getValue());
		if(orc.getOrc11_VerifiedBy(0).getIDNumber().getValue()!=null)
			req.setVerifyedByID(orc.getOrc11_VerifiedBy(0).getIDNumber().getValue());
		if(rqd.getRqd2_ItemCodeInternal().getCe1_Identifier().getValue()!=null)
			req.setDrugID(Integer.parseInt(rqd.getRqd2_ItemCodeInternal().getCe1_Identifier().getValue()));
		if(rqd.getRqd2_ItemCodeInternal().getCe2_Text().getValue()!=null)
			req.setDrugName(rqd.getRqd2_ItemCodeInternal().getCe2_Text().getValue());
		if(rqd.getRqd5_RequisitionQuantity().getValue()!=null)
			req.setQuantity(Integer.parseInt(rqd.getRqd5_RequisitionQuantity().getValue()));
		if(rqd.getRqd6_RequisitionUnitOfMeasure().getCe2_Text().getValue()!=null)
			req.setType(rqd.getRqd6_RequisitionUnitOfMeasure().getCe2_Text().getValue());
		if(rqd.getRqd10_DateNeeded()!=null){
			try {
				req.setRequisitionDate(this.toDate(rqd.getRqd10_DateNeeded().getYear(), rqd.getRqd10_DateNeeded().getMonth(), rqd.getRqd10_DateNeeded().getDay()));
			} catch (DataTypeException e) {
				// TODO Auto-generated catch block
				req.setRequisitionDate(null);
			}
		}
		
		
		
	}
	
	@SuppressWarnings("deprecation")
	private Date toDate(int yy, int mm, int dd) throws DataTypeException{
		Date date = new Date(yy,mm,dd);
		return date;
		
	}
	
	/*an OMN_O07 example
	 * MSH|^~\\&|GUI|CSTH|PHA|CSTH|20110610101010||OMN^O07||P|2.5.1\r
	 * ORC|NE|<requisition id>||||E||||<entered by id>|<verified by id>\r
	 * RQD||<drug id>^<Name>|||<quantity>|^<type>||||<date>\r */
	

}
